//
//  DiaoDuListVC.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2020/8/19.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "DiaoDuListVC.h"
//#import "DiaoDuMainModel.h"
//#import "SupplierMainModel.h"
#import "CarMainModel.h"
#import "DiaoDuListCell.h"
#import "DiaoDuEditVC.h"

@interface DiaoDuListVC ()<UITableViewDelegate,UITableViewDataSource>{
    int page;
}

@property (nonatomic, strong) NSMutableArray <CarMainModel *>*array;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *bottomView;


@end

@implementation DiaoDuListVC

-(NSMutableArray<CarMainModel *> *)array {
    if (!_array) {
        _array = [NSMutableArray array];
//        DiaoDuMainModel *model1 = [DiaoDuMainModel new];
//        model1.carNO = @"苏E 13H13";
//        model1.carType = @"货的";
//        SupplierMainModel *sModel1 = [SupplierMainModel new];
//        sModel1.name = @"3PL1";
//        model1.suppliers = [NSMutableArray arrayWithArray:@[sModel1,sModel1,sModel1,sModel1,sModel1,sModel1,sModel1,sModel1,sModel1,sModel1]];
//        DiaoDuMainModel *model2 = [DiaoDuMainModel new];
//        model2.carNO = @"苏E 13H14";
//        model2.carType = @"货的";
//        model2.suppliers = [NSMutableArray arrayWithArray:@[sModel1,sModel1,sModel1,sModel1,sModel1,sModel1,sModel1,sModel1]];
//        DiaoDuMainModel *model3 = [DiaoDuMainModel new];
//        model3.carNO = @"苏E 13H15";
//        model3.carType = @"卡车";
//        model3.suppliers = [NSMutableArray arrayWithArray:@[sModel1,sModel1,sModel1,sModel1,sModel1,sModel1,sModel1,sModel1]];
//        [_array addObject:model1];
//        [_array addObject:model2];
//        [_array addObject:model3];
    }
    return _array;
}

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 64, ScreenW, 64+BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
        UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, _bottomView.nim_width - 30 - 30, 44)];
        [nextBtn setBackgroundColor:HexColorA(0x3D68EB, 0.1)];
        nextBtn.layer.masksToBounds = YES;
        nextBtn.layer.cornerRadius = 6;
        [nextBtn setTitle:@"批量修改可调度供应商" forState:0];
        [nextBtn setTitleColor:HexColor(0x3D68EB) forState:0];
        nextBtn.titleLabel.font = BoldFontOfSize(16);
        [nextBtn addTarget:self action:@selector(nextClick) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:nextBtn];
    }
    return _bottomView;
}

- (void)nextClick {
    DiaoDuEditVC *vc = [DiaoDuEditVC new];
    [self.rt_navigationController pushViewController:vc animated:YES];
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - 64 - BottomHeight)];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page = 1;
            [strongSelf requestForList];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page++;
            [strongSelf requestForList];
        }];
        _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"img_occupy"
             titleStr:@""
            detailStr:@""];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"供应商调度";
    page = 1;
    self.view.backgroundColor = HexColor(0xffffff);
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.tableView];
    [self requestForList];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:ReceiptDeliveryRefreshCarTPLList object:nil];
}

- (void)refreshData {
    page = 1;
    [self requestForList];
}

- (void)requestForList {
    [HttpRequest PostWithApi:LogisticsSupplierCarList params:@{@"page":@(page)} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            if (self->page == 1) {
                [self.array removeAllObjects];
                self.array = [CarMainModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
            }else {
                NSArray *array = [CarMainModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
                [self.array addObjectsFromArray:array];
            }
            BOOL has_more = [responseObject[@"has_more"] boolValue];
            if (has_more) {
                self.tableView.mj_footer.hidden = NO;
            }else {
                self.tableView.mj_footer.hidden = YES;
            }
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DiaoDuListCell *cell = [DiaoDuListCell DiaoDuListCellWithTableView:tableView];
    cell.model = self.array[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    DiaoDuListCell *cell = [DiaoDuListCell DiaoDuListCellWithTableView:tableView];
    cell.model = self.array[indexPath.row];
    return cell.height;
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
